var LevelWin = {
    _util: new Util(),
    _manage: null,
    _refer: null,
    _winLayer: null,
    _txtScore: {
        key: "txtScore"
    },
    _sprNew: {
        key: "sprNew"
    },
    init: function(){
        this._util.onAssignCCBMemberVariable(this.assignActive());
        var layerNode = this._util.newLayerFromCCB(s_levelWin_ccbi, this._refer);
        this._winLayer = layerNode;
        this._manage = this._util.ccbReader.getAnimationManager();
        this._manage.setCompletedAnimationCallback(this, this.onCCBAnimEnd);
        
        if (layerNode != null) {
            this._refer.addChild(layerNode, 2);
        }
    },
    onEnter: function(refer){
        cc.log("level_win -> onEnter")
        this._refer = refer;
        
        var newHighscore = false;
        var lastScore = game.getScore();
        var curScore = storage.get('levelScore');
        if (curScore > lastScore) {
            game.setScore(curScore);
            newHighscore = true;
        }
        
        game.completeLevel(refer._level);
        
        this.init();
        
        this._txtScore.instance.setString(curScore);
        if (newHighscore) {
            this._sprNew.instance.setVisible(true);
        } else {
            this._sprNew.instance.setVisible(false);
        }
        
        this.outAction = null;
    },
    onMainMenu: function(){
        if (this._manage.getRunningSequenceName() != "Out") {
            var $this = this;
            this._manage.runAnimations("Out");
            this.outAction = function(){
                $this.exitGame();
                game.enterSelectLevels();
            }
        }
    },
    onRetry: function(){
        if (this._manage.getRunningSequenceName() != "Out") {
            var $this = this;
            this._manage.runAnimations("Out");
            this.outAction = function(){
                $this._winLayer.removeFromParent(true);
                $this._refer.resumeLevel();
                $this._refer.retryLevel();
            }
        }
    },
    onNextLevel: function(){
        if (this._manage.getRunningSequenceName() != "Out") {
            var $this = this;
            var goto_next_level = function(_nextLevel){
                $this.exitGame();
                $this._refer.resumeLevel();
                $this._refer._level = _nextLevel;
                $this._refer.runLevel();
            }
            var nextPlay = game.getNextPlayLevel(this._refer._level);
            if (nextPlay > 0) {
                this._manage.runAnimations("Out");
                this.outAction = function(){
                    goto_next_level(nextPlay);
                }
            } else if (nextPlay == 0) {
                cc.log('no more unlocked levels, need unlock')
            } else {
                cc.log('no more levels, all level passed!');
                this._manage.runAnimations("Out");
                this.outAction = function(){
                    $this.exitGame();
                    $this._refer.setState($this._refer._levelAllDone);
                }
            }
        }
    },
    exitGame: function(){
        this._winLayer.removeFromParent(true);
        this._refer.exitLevel();
    },
    assignActive: function(){
        var arr = new Array();
        var $this = this;
        arr.push({
            key: "btnRetry",
            callback: function(){
                sound.playButtonSound();
                $this.onRetry();
            }
        });
        arr.push({
            key: "btnMainMenu",
            callback: function(){
                sound.playButtonSound();
                $this.onMainMenu();
            }
        });
        arr.push({
            key: "btnNextLevel",
            callback: function(){
                sound.playButtonSound();
                $this.onNextLevel();
            }
        });
        arr.push(this._txtScore);
        arr.push(this._sprNew);
        return arr;
    },
    outAction: null,
    onCCBAnimEnd: function(){
        var animName = this._manage.getLastCompletedSequenceName();
        if (animName == "Out") {
            if (this.outAction != null) {
                this.outAction();
            }
        }
    }
}
